<?php

namespace IYUU\sdk\CoolQ\Domain;

use BadRequestException;
use BlackListModel;
use Exception;
use UserModel;

/**
 * 黑名单
 * Class BlackList
 * @package IYUU\sdk\CoolQ\Domain
 */
class BlackList
{
    /**
     * @param int|string $qq
     * @return array
     * @throws BadRequestException
     */
    public static function check($qq): array
    {
        if (!empty($qq)) {
            try {
                // 1. 是否存在QQ用户
                $user = UserModel::where('user_id', $qq)->findOrEmpty();
                if ($user->isEmpty()) {
                    return [];
                }
                $uid = $user->id;

                // 2. 查询黑名单
                $black_list = BlackListModel::where('uid', $uid)->findOrEmpty();
                if ($black_list->isEmpty()) {
                    return [];
                }
                return $black_list->toArray();
            } catch (Exception $ex) {
                // 数据库出错啦
                throw new BadRequestException($ex->getMessage(), $ex->getCode());
            }
        }
        return [];
    }
}
